﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Reflection;
using System.Net.Sockets;
using System.Configuration;
using System.IO;
using System.Net;
namespace 校园网小工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static bool isFree=true;
        public static bool shouldConnect = true;
        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                if (comboBox1.Text == "模式一")
                {
                    VPNHelper vpnhelper = new VPNHelper();
                    vpnhelper.CreateOrUpdateVPN("DontDeleteMe", "210.42.151.73");
                    vpnhelper.TryConnectVPN("DontDeleteMe", "newthread", "newthread");
                    button1.Enabled = false;
                    button2.Enabled = true;
                    MessageBox.Show("成功！本程序会在右下角后台运行，双击可再次呼出！");
                    this.WindowState = FormWindowState.Minimized;
                    this.Hide();
                    notifyIcon1.Visible = true;

                }
                else if (comboBox1.Text == "模式二")
                {
                    VPNHelper vpnhelper = new VPNHelper();
                    vpnhelper.CreateOrUpdateVPN("DontDeleteMe", "210.42.151.15");
                    vpnhelper.TryConnectVPN("DontDeleteMe", "newthread", "newthread");
                    button1.Enabled = false;
                    button2.Enabled = true;
                    MessageBox.Show("成功！本程序会在右下角后台运行，双击可再次呼出！");
                    this.WindowState = FormWindowState.Minimized;
                    this.Hide();
                    notifyIcon1.Visible = true;

                }
                else MessageBox.Show("你都没选模式。");
            }
            catch
            {
                MessageBox.Show("人品不好，失败了，程序将自动退出。");
                Application.Exit();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                VPNHelper vpnhelper = new VPNHelper();
                vpnhelper.TryDisConnectVPN("DontDeleteMe");
                button1.Enabled = true;
                button2.Enabled = false;
                MessageBox.Show("成功停止！");
            }
            catch
            {
                MessageBox.Show("人品不好，失败了，程序将自动退出。");
                Application.Exit();
            }
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string target = "http://www.crackerben.com";
            try
            {
                System.Diagnostics.Process.Start(target);
            }
            catch (System.ComponentModel.Win32Exception noBrowser)
            {
                if (noBrowser.ErrorCode == -2147467259)
                    MessageBox.Show(noBrowser.Message);
            }
            catch (System.Exception other)
            {
                MessageBox.Show(other.Message);
            } 
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
            
            
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            switch(this.WindowState)
            {
                case FormWindowState.Minimized: 
                   this.WindowState = FormWindowState.Minimized;    //使关闭时窗口向右下角缩小的效果 
                   this.Hide();
                   notifyIcon1.Visible = true;
                   break;
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            VPNHelper vpnhelper = new VPNHelper();
            vpnhelper.TryDisConnectVPN("DontDeleteMe");
            Application.Exit();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            VPNHelper vpnhelper = new VPNHelper();
            vpnhelper.TryDisConnectVPN("DontDeleteMe");
            Application.Exit();
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
           
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)  //鼠标左键
            {
                Type t = typeof(NotifyIcon);
                MethodInfo mi = t.GetMethod("ShowContextMenu", BindingFlags.NonPublic | BindingFlags.Instance);
                mi.Invoke(this.notifyIcon1, null);
            }    
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (shouldConnect)
                {
                    string ip = "210.42.151.54";
                    int port = 9001;
                    TcpClient tc = new TcpClient();
                    tc.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
                    Stream sStream = tc.GetStream();
                    BinaryReader temp = new BinaryReader(sStream);
                    byte[] buffer = new byte[100];
                    temp.Read(buffer, 0, buffer.Length);
                    string imformation = Encoding.UTF8.GetString(buffer).ToString().ToString().Replace("\0", null);
                    if (imformation == "false")
                    {
                        isFree = false;
                    }
                }
                else
                {

                }
            }
            catch
            {

            }
           
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (isFree == false)
            {
                VPNHelper vpnhelper = new VPNHelper();
                vpnhelper.TryDisConnectVPN("DontDeleteMe");
                this.Hide();
                LoginForm lf = new LoginForm();
                lf.Show();
            }

            webBrowser1.Navigate("http://xk.scuec.edu.cn/xsxk/logout.xk");
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (webBrowser1.Document.Cookie != null)
            Static.cookie = webBrowser1.Document.Cookie;           
            ChooseClass fm=new ChooseClass();
            fm.Show();
        }


    }
}
